<template>
    <div></div>
</template>
<script setup>
import { ref, onMounted } from "vue";
import { tset_login, user_login } from "@/api/login";
import { app_visit } from "@/api/list";
import { showLoadingToast } from "vant";

//授权
const login = () => {
    let appid = "wwdc0bc731d475dfb8";
    let redirect_uri = "https://wechatwork.ihg.com.cn/static/commercial/index.html";
    let url = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${encodeURI(appid)}&redirect_uri=${encodeURI(
        redirect_uri
    )}&response_type=code&scope=snsapi_base#wechat_redirect`;
    window.location.replace(url);
};

//获取code
const getQueryVariable = (variable) => {
    const search = window.location.search || window.location.hash.split("?")[1] || "";
    const params = new URLSearchParams(search);
    return params.get(variable);
};

//测试登录
const testLogin = async (user_id) => {
    const res = await tset_login({ user_id: user_id });
    //存token
    localStorage.setItem("token", res.data);

    //添加访问记录
    await app_visit({ app_id: "9999" });

    window.location.replace("/static/commercial/#/List");
};

//登录
const getLogin = async (code) => {
    let res = await user_login({
        code: code,
    });
    //存token
    localStorage.setItem("token", res.data);

    //添加访问记录
    await app_visit({ app_id: "9999" });

    window.location.replace("/static/commercial/#/List");
};

onMounted(() => {
    if (getQueryVariable("code")) {
        showLoadingToast({
            message: "加载中...",
            forbidClick: true,
        });
        //生产
        getLogin(getQueryVariable("code"));
        /**
         * 开发时使用 测试 http://localhost:5173/static/commercial/#/Login?code=47686
         * 47686 总管理员
         */
        // testLogin(getQueryVariable("code"));
    } else {
        login();
    }
});
</script>
<style lang="scss" scoped></style>
